﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SService
{
    public class SalesreturnS : SalesreturnSI
    {
        private readonly WmssystemContext myDb;

        public SalesreturnS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }

        public async Task<int> Add(PO_Salesreturndetails pO_Salesreturndetails)
        {
            myDb.PO_Salesreturndetails.Add(pO_Salesreturndetails); // 确保不显式指定标识列的值
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> Add1(PO_Salesreturn pO_Salesreturn)
        {
            myDb.PO_Salesreturn.Add(pO_Salesreturn); // 确保不显式指定标识列的值
            return await myDb.SaveChangesAsync();
        }

        public Task<List<PO_Salesreturn>> Fye(int pageSize, int currentPage, string Number = null, string Status = null, string Supplier = null, DateTime? date = null, DateTime? date1 = null)
        {
            var query = myDb.PO_Salesreturn.AsQueryable();
            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Number))
            {
                query = query.Where(c => c.S_RReturnOrder.Contains(Number));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.S_RDocumentStatus == Status);
            }
            if (!string.IsNullOrEmpty(Supplier))
            {
                query = query.Where(c => c.S_RCustomerName == Supplier);
            }
            if (date != null)
            {
                query = query.Where(e => e.S_RReturnDate >= date);
            }
            if (date1 != null)
            {
                query = query.Where(e => e.S_RReturnDate <= date1);
            }

            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public Task<List<PO_Salesreturn>> Qu()
        {
          return  myDb.PO_Salesreturn.ToListAsync();
        }

        public async Task<int> XsDel(List<int> Ids)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToDelete = await myDb.PO_Salesreturn
                .Where(d => Ids.Contains(d.S_RIndex))
                .ToListAsync();

            // 删除查询结果
            myDb.PO_Salesreturn.RemoveRange(detailsToDelete);

            // 保存删除操作到数据库
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> XsShen(PO_Salesreturn pO_Salesreturn, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDb.PO_Salesreturn.FindAsync(pO_Salesreturn.S_RIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.S_RDocumentStatus = "已审核";
                //把货品退货的数量回写到销售订单明细表中
                var ppList = await myDb.PO_Salesreturndetails.Where(c => c.S_RReturnOrder == purchaseOrder.S_RReturnOrder).ToListAsync();
                var bbList = await myDb.PO_Salesorderdetail.Where(c=>c.S_DSaCode==purchaseOrder.S_RSalesOrder).ToListAsync();
                for (int i = 0; i < ppList.Count; i++)
                {
                    if (i < bbList.Count)
                    {
                        var pp = ppList[i];
                        var bb = bbList[i];
                        bb.S_DQuantitySold = pp.Sr_SalesQuantity + pp.Sr_ReturnQuantity;
                    }
                }

            }
            else
            {
                purchaseOrder.S_RDocumentStatus = "待审核";
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.S_RApprover = "admin"; // 以此类推，更新需要修改的字段
            purchaseOrder.S_RApproverTime = DateTime.Now;
            purchaseOrder.P_Reviewcomments = pO_Salesreturn.P_Reviewcomments;


            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Salesreturn.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }
    }
}
